import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/layout',
    name: 'layout',
    redirect: '/layout/home',
    component: () => import('@/views/layout'),
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('@/views/layout/home'),
        meta: {
          title: '搜索'
        }
      },
      {
        path: 'ask',
        name: 'ask',
        component: () => import('@/views/layout/ask'),
        meta: {
          title: '问答'
        }
      },
      {
        path: 'video',
        name: 'video',
        component: () => import('@/views/layout/video'),
        meta: {
          title: '视频'
        }
      },
      {
        path: 'user',
        name: 'user',
        component: () => import('@/views/layout/user'),
        meta: {
          title: '个人中心'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('@/views/layout/Search'),
    meta: {
      title: '搜索'
    }
  },
  {
    path: '/article/:articleId',
    name: 'article',
    component: () => import('@/views/article'),
    // 开启props路由传参
    props: true
  },
  {
    path: '/user/modify',
    name: 'userModify',
    component: () => import('@/views/modifyUserInfo'),
    meta: {
      title: '个人资料'
    }
  },
]

const router = new VueRouter({
  routes
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
